package pizza.admin.controller;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pizza.admin.dao.PizzaDAO;
import pizza.admin.dao.UserDAO;
import pizza.admin.entity.User;

/**
 * Servlet implementation class UserController
 */
@WebServlet("/user")
public class UserController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		UserDAO userDAO = new UserDAO();
		String action = request.getParameter("action");
		HttpSession session = request.getSession(true);
		User user = new User();
		Collection<User> allUsers = null;

		if (action != null) {
			if (session.getAttribute("user") == null) {
				request.getRequestDispatcher("/templates/index.jsp").forward(
						request, response);
			}
			if (action.equals("logout")) {

				session.removeAttribute("user");
				request.getRequestDispatcher("/templates/index.jsp").forward(
						request, response);
			} else if (action.equals("add")) {

				request.getRequestDispatcher("/templates/user/add.jsp")
						.forward(request, response);
			} else if (action.equals("edit")) {
				user = userDAO.getById(Integer.parseInt(request
						.getParameter("id")));
				user.setPassword("");
				request.setAttribute("user", user);

				request.getRequestDispatcher("/templates/user/edit.jsp")
						.forward(request, response);
			} else if (action.equals("delete")) {

				user = userDAO.getById(Integer.parseInt(request
						.getParameter("id")));
				userDAO.delete(user);

//				userDAO.initializeEntityManager();
				allUsers = userDAO.getAll();
				request.setAttribute("allUsers", allUsers);
				request.getRequestDispatcher("/templates/user/list.jsp")
						.forward(request, response);
			} else if (action.equals("list")||action.isEmpty()) {

				allUsers = userDAO.getAll();
				request.setAttribute("allUsers", allUsers);
				request.getRequestDispatcher("/templates/user/list.jsp")
						.forward(request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		UserDAO userDAO = new UserDAO();
		String action = request.getParameter("action");

		HttpSession session = request.getSession(true);
		User user = new User();

		if (action != null) {
			if(!action.equals("login")){
				if (session.getAttribute("user") == null) {
					request.getRequestDispatcher("/templates/index.jsp").forward(
							request, response);
				}
			}
			if (action.equals("login")) {

				user = userDAO.login(request.getParameter("username"),
						request.getParameter("password"), true);
				if (user.getId() != null) {
					session.setAttribute("user", user);
					request.getRequestDispatcher("/templates/welcome.jsp")
							.forward(request, response);
				} else {
					request.setAttribute("error", "error");
					request.getRequestDispatcher("/templates/index.jsp")
							.forward(request, response);
				}
			} else if (action.equals("add")) {

				user.setName(request.getParameter("name"));
				user.setSurname(request.getParameter("surname"));
				user.setUsername(request.getParameter("username"));
				user.setPassword(request.getParameter("password"));
				user.setPasswordRepeat(request.getParameter("passwordRepeat"));
				user.purifyFields();
				
				if (!user.validateForDB()) {
					request.setAttribute("user", user);
					request.getRequestDispatcher("/templates/user/add.jsp")
							.forward(request, response);
				} else {
					userDAO.add(user);
					Collection<User> allUsers = null;
//					userDAO.initializeEntityManager();
					allUsers = userDAO.getAll();
					request.setAttribute("allUsers", allUsers);
					request.getRequestDispatcher("/templates/user/list.jsp")
							.forward(request, response);
				}
			} else if (action.equals("edit")) {
				user = userDAO.getById(Integer.parseInt(request
						.getParameter("id")));
				String oldPass = user.getPassword();
//				userDAO.initializeEntityManager();
				
				user.setName(request.getParameter("name"));
				user.setSurname(request.getParameter("surname"));
				user.setUsername(request.getParameter("username"));
				user.setPassword(request.getParameter("password"));
				user.setPasswordRepeat(request.getParameter("passwordRepeat"));
				user.purifyFields();
				
				if (!user.validateForDB()) {
					request.setAttribute("user", user);
					request.getRequestDispatcher("/templates/user/add.jsp")
							.forward(request, response);
				} else {
					if(!user.getPassword().isEmpty()){
						user.encodePasswordForDB();
					}else{
						user.setPassword(oldPass);
					}
					
					userDAO.edit(user);
					Collection<User> allUsers = null;
//					userDAO.initializeEntityManager();
					allUsers = userDAO.getAll();
					request.setAttribute("allUsers", allUsers);
					request.getRequestDispatcher("/templates/user/list.jsp")
							.forward(request, response);
				}
			}
		}
	}

}
